package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.sc.nft.dao.ExportRecordsDao;
import com.sc.nft.dao.TempAirdropRecordDao;
import com.sc.nft.entity.ExportRecords;
import com.sc.nft.entity.TempAirdropRecord;
import com.sc.nft.entity.vo.AirdropGrantRecordExportListVO;
import com.sc.nft.enums.ExportRecordsTypeEnum;
import com.sc.nft.enums.ExportTypeEnum;
import com.sc.nft.enums.ExportTypeStatusEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.mapper.TempAirdropRecordMapper;
import com.sc.nft.pool.SysMemberPool;
import com.sc.nft.service.AirdropGrantRecordService;
import com.sc.nft.service.TempAirdropRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @author 朱梦晨
 * @Description:
 * @date 2024-07-27
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class TempAirdropRecordServiceImpl implements TempAirdropRecordService {

    private final TempAirdropRecordMapper tempAirdropRecordMapper;

    private final TempAirdropRecordDao tempAirdropRecordDao;

    private final ExportRecordsDao exportRecordsDao;

    private final AirdropGrantRecordService airdropGrantRecordService;


    @Override
    public Boolean saveTempAirdropRecord(Date startTime, Date endTime, String collectionName) {
        Long exportRecordsId = getExportRecordsId();
        List<AirdropGrantRecordExportListVO> airdropGrantRecordVOList = airdropGrantRecordService.airdropGrantRecordListExport(collectionName, startTime, endTime);
        List<TempAirdropRecord> tempAirdropRecords = BeanUtil.copyToList(airdropGrantRecordVOList, TempAirdropRecord.class);
        if (CollectionUtils.isEmpty(tempAirdropRecords)) {
            throw new GlobalRunTimeException("导出数据为空");
        }
        try {
            tempAirdropRecords.forEach(t -> t.setExportRecordsId(exportRecordsId));
            tempAirdropRecordMapper.saveBatch(tempAirdropRecords);
        } catch (Exception e) {
            //导出失败
            log.error("空投明细记录导出失败:", e);
            return exportRecordsDao.updateStatusById(exportRecordsId, ExportTypeStatusEnum.EXPORT_STATUS_FAIL.getCode());
        }
        return exportRecordsDao.updateStatusById(exportRecordsId, ExportTypeStatusEnum.EXPORT_STATUS_SUCCESS.getCode());
    }


    private static Long getExportRecordsId() {

        StringBuilder exportName = new StringBuilder();
        int exportType = 0;
        exportName.append(ExportTypeEnum.TEMP_AIRDROP.getName()).append(DateUtil.format(DateUtil.date(), "yyyy年MM月dd日")).append(".xlsx");
        exportType = ExportTypeEnum.TEMP_AIRDROP.getCode();
        //增加导出记录
        ExportRecords exportRecords = new ExportRecords();
        exportRecords.setExportName(exportName.toString());
        exportRecords.setExportType(exportType);
        exportRecords.setStatus(ExportTypeStatusEnum.EXPORT_STATUS_IN.getCode());
        exportRecords.setCreatorId(SysMemberPool.getMember().getId());
        exportRecords.setType(ExportRecordsTypeEnum.ADMIN);
        exportRecords.insert();
        Long exportRecordsId = exportRecords.getId();
        return exportRecordsId;
    }


    /**
     * 下载  根据导出记录Id
     *
     * @param exportRecordsId
     * @return
     */
    @Override
    public List<TempAirdropRecord> findTempAirdropRecordList(Long exportRecordsId) {
        return tempAirdropRecordDao.getTempAirdropRecordList(exportRecordsId);
    }

}
